package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        //由于页面没有给创建时间和状态添加对应输入框，因此需要手动设置默认值或手动进行赋值操作
        appointment.setCreateTime(new Date());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        //由于页面没有给创建时间和状态添加对应输入框，因此需要手动设置默认值或在sql语句中把更新的创建时间和状态字段删除掉
        //方式1：删除对应sql语句中没有进行编辑的相关字段（SQL中需要注意，只更新需要修改的字段.）
        //方式2：先查，在替换，最后再更新
        Appointment at = this.get(appointment.getId());
        //只有状态为预约状态才可以进行编辑
        if(!Appointment.STATUS_APPOINTMENT.equals(at.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行编辑");
        }
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }


    /**
     * 软删除
     * @param id
     */
    @Override
    public void softRemove(Long id) {
        //只有是预约状态时才允许删除
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行删除操作");
        }
        appointmentMapper.softRemove(id);
    }

    @Override
    public void cancel(Long id) {
        //只有是预约状态时才允许取消
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态才可以进行取消操作");
        }
        appointmentMapper.changeStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public void arrival(Long id) {
        Appointment appointment = this.get(id);
        if(!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new BusinessException("只有是预约状态中才可以修改为到店状态");
        }
        //通过id将状态修改为已到店，并且还要将到店时间修改为当前时间
        // 可以给当前方法加一个时间参数，也可以在sql中使用函数来表示当前时间
        appointmentMapper.arrival(id,Appointment.STATUS_ARRIVAL);
    }



}
